package com.cvte.boe.media.controller;

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;


/**
 * Created by wuguitong on 2018/4/13.
 */
public class LRUThumbManager {
    private volatile static LRUThumbManager instance = null;
    private LruCache<String, Bitmap> mThumbnaiCache;
    public LRUThumbManager() {
        int mCacheSize = 8 * 1024 * 1024; //8M thumbnail cache;
        mThumbnaiCache = new LruCache<String, Bitmap>(mCacheSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount();
            }
        };
    }
    public static LRUThumbManager getInstance(){
        if(instance == null){
            synchronized (LRUThumbManager.class){
                if(instance == null){
                    instance = new LRUThumbManager();
                }
            }
        }
        return instance;
    }
    public void putImageThumb(String key,Bitmap bitmap){
        synchronized (this) {
            if ((key == null) || (bitmap == null)) return;
            mThumbnaiCache.put(key, bitmap);
        }
    }
    public Bitmap getImageThumb(String key){
        synchronized (this) {
            return mThumbnaiCache.get(key);
        }
    }
    public void cleanThumb(){
        synchronized (this) {
            mThumbnaiCache.evictAll();
        }
    }
}
